﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DotNet.WinUI.Basic
{
    public partial class FrmIO : Form
    {
        public FrmIO()
        {
            InitializeComponent();
        }

        private void btnDirectory_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(txtDirectory.Text);
        }

        private void btnWriterFile_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(txtDirectory.Text))
            {
                //创建写入流对象
                StreamWriter objWriter = new StreamWriter(txtDirectory.Text, true, Encoding.GetEncoding("GB2312"));
                //将缓冲区刷新到基础流
                objWriter.AutoFlush = true;
                //写文件操作
                objWriter.WriteLine(txtContent.Text);
                //关闭写入对象
                objWriter.Close();
            }
            else
            {
                MessageBox.Show("文件不存在！");
            }
        }

        private void btnReaderFile_Click(object sender, EventArgs e)
        {
            StreamReader objReader = new StreamReader(txtDirectory.Text, Encoding.GetEncoding("GB2312"));
            txtContent.Text = objReader.ReadToEnd();
            objReader.Close();
        }

        private void btnPathFile_Click(object sender, EventArgs e)
        {
            try
            {
                // Only get files that begin with the letter "c."
                string[] dirs = Directory.GetFiles(txtDirectory.Text);
                foreach (string dir in dirs)
                {
                    txtContent.Text += dir + "\n";
                }
            }
            catch (Exception ex)
            {
               txtContent.Text = ex.ToString();
            }
        }
    }
}
